<template>
  <div>
    <div class="app-container clearfix pageHeader">
      <div class="header fl">{{$route.meta.title}}</div>
    </div>
    <div class="app-container margin-content padding-content">
      <div class="filter-container">
        <el-button class="filter-item" icon="el-icon-edit" type="primary" @click="handleCreate" size="mini">新建菜单
        </el-button>
        <!--<el-button class="filter-item" type="danger" icon="el-icon-delete" size="mini" >删除</el-button >-->
        <!--<el-button class="filter-item" type="default" size="mini" @click="handleOrder" ><span-->
        <!--v-if="isShowEdit" >取消</span >排序-->
        <!--</el-button >-->
        <!--<el-button class="filter-item" type="info" size="mini" v-if="isShowEdit" >保存排序</el-button >-->
        <div class="fr">
          <el-input @keyup.native="handleQuery" placeholder="请输入菜单名称" v-model="query"
                    style="width:200px;" @keypress.native='handleQuery'
                    size="mini">
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
            <!--<template slot="append" >-->
            <!--<i slot="prefix" class="el-input__icon el-icon-search" ></i >-->
            <!--<el-button icon="el-icon-search" @click="handleQuery" ></el-button >-->
            <!--</template >-->
          </el-input>
        </div>
      </div>


      <tree-table :height="scrollHeight" :data="data" :evalFunc="func" :evalArgs="args" :expandAll="expandAll" border
                  :labelName="'菜单名称'">
        <!--<el-table-column-->
        <!--type="selection"-->
        <!--slot="firstColumn"-->
        <!--width="40" >-->
        <!--</el-table-column >-->
        <el-table-column label="菜单路径" width="200">
          <template slot-scope="scope">
            <span v-if="!isShowEdit">{{scope.row.url}}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否隐藏" width="80">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.locked) === 0" type="success">显示</el-tag>
            <el-tag v-else type="info">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200"  fixed="right">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit(scope.row.id)">编辑</el-button>
            <divider type="vertical"></divider>
            <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </tree-table>
    </div>
    <!--创建菜单对话框-->
    <menu-dialog ref="menuDialog" @submited-menu="submitedMenu" :menusOptions="orgianlData"></menu-dialog>
  </div>
</template>

<script>
  import treeTable from '@/components/TreeTable'
  import treeToArray from './customEval'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  import { listRouter, addRouter, batchDelRouter, getDetail, update } from '@/api/menus'
  import menuDialog from './components/menuDIalog.vue'
  import { modifyObj, filterMenus } from '@/utils/tools'
  import Divider from '@/components/Divider'

  const _import = require('@/router/_import_' + process.env.NODE_ENV)
  /* Layout */
  import Layout from '@/views/layout/Layout'

  export default {
    name: 'menusMain',
    components: {
      treeTable,
      Treeselect,
      menuDialog,
      Divider
    },
    data() {
      return {
        isShowEdit: false,
        scrollHeight: null,
        formData: {
          id: null,
          name: null,
          parentId: null
        },
        query: '',
        func: treeToArray,
        expandAll: true,
        listLoading: false,
        tableKey: 0,
        rules: {name: [{required: true, message: '请填写菜单名', trigger: 'blur'}]},
        data: [],//菜单数据
        orgianlData: [],//原始的菜单数据，未经过裁剪
        args: [null, null, 'orderNo'],
        routerArr: []
      }
    },
    created() {
      this.getMenus()
      this.tableResize()
    },
    methods: {
      tableResize(){
        const _height = 285
        this.scrollHeight = document.body.scrollHeight - _height
        window.onresize = () => {
          this.scrollHeight = document.body.scrollHeight - _height
        }
      },
      message(row) {
        this.$message.info(row.name)
      },

      handleCreate() {
        this.$refs.menuDialog.handleCreate() //打开子组件对话框
      },
      handleEdit(id) {
        this.$refs.menuDialog.handleEdit(id) //打开子组件对话框
      },
      handleDelete(obj) {
        if (obj.children !== undefined) {
          this.$alert('该节点下存在子菜单，请先删除子菜单', '提示', {
            confirmButtonText: '确定',
          })
          return false
        }
        this.$confirm('此操作将永久删除该机构, 是否继续?', '提示', {
          confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning'
        }).then(() => {
          batchDelRouter({ids: obj.id}).then(res => {
            res = res.data
            this.getMenus()
            this.query = ''
            this.dialogFormVisible = false
            this.$message({showClose: true, type: 'success', message: '删除成功!'});

          })
        }).catch(() => {
          this.$message({type: 'info', message: '已取消删除'});
        });
      },
      getMenus() {
        listRouter().then(res => {
          res = res.data
          this.orgianlData = res.data
          this.data = res.data
        })
      },
      handleQuery() {
        if (this.query !== '') {
          let result = filterMenus(this.orgianlData, this.query)
          this.data = result
        } else {
          this.getMenus()
        }
      },
      submitedMenu(res) {
        this.getMenus()
        this.query = ''
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped="scope">
  .el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: center;
    line-height: 60px;
  }

  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
    line-height: 200px;
  }

  .el-main {
    background-color: #E9EEF3;
    color: #333;
    text-align: center;
    line-height: 160px;
  }

  body > .el-container {
    margin-bottom: 40px;
  }

  .el-container:nth-child(5) .el-aside,
  .el-container:nth-child(6) .el-aside {
    line-height: 260px;
  }

  .el-container:nth-child(7) .el-aside {
    line-height: 320px;
  }

  .el-header {
    background: #fff;
  }

  .el-main {
    background: #fff;
  }
</style>
